/**
 * @class common.base.listView
 * @extends common.base.object
 * 列表视图基类
 */
define(function (require) {
    require('routie');
    var Pagination = require('common/pagination');
    var utils = require('common/utils/usercenter');
    var BaseView = require('common/base/view');
    var EmptyTpl = require('text!./templates/listEmpty.html');
    var BaseEditTpl = require('text!./templates/edit.html');

    var BaseListView = BaseView.extend({
        pageWrap: '.pagination_wrap',
        data: null,
        tpl: null,
        itemTpl: null,
        initialize: function (params) {
            var _this = this;
            _this.el = '.app_' + _global.m;
            BaseView.prototype.initialize.apply(_this, [params]);
        },
        parseTab: function () {
            var _this = this;
            //处理nav-tabs
            _this.$('.JNavTabs a').each(function () {
                var data = $(this).data();
                $(this).attr('href', '#list&' + _.http_build_query(data));
            });
        },
        handlePage: function () {
            var _this = this;
            if (_this.pageWrap) {
                _this.page = new Pagination({wrap: _this.pageWrap});
            }
        },
        fetch: function (params, callback) {
            var _this = this;
            utils.ajaxGet(params, function (resp) {
                callback.apply(_this, [resp]);
            });
        },
        /**
         * 重新触发加载页面路由
         */
        reload: function () {
            var hash = _.parse_hash();
            hash.t = _.time();
            window.location.href = window.location.origin + window.location.pathname + window.location.search + '#list&' + _.http_build_query(hash);
        },
        /**
         * 路由配置
         * @type {Object}
         */
        routers: {
            '': function () {
                BaseListView.prototype.routie('list');
            },
            'list*': "handleList",
            'add': '_handleEdit',
            'edit/*': '_handleEdit'
        },
        _handleEdit: function (id) {
            var _this = this;
            var is_add = window.location.hash.indexOf('add') == 1;
            var id = _.intval(id);
            utils.ajaxGet({m: _global.m, a: 'edit', id: id }, function (resp) {
                _this.setAppContent(BaseEditTpl, {tab_name: (is_add ? '添加' : '编辑') + _this.title});
                $('.JListTable').removeClass('loading');
                _this.render(_this.editTpl, resp, $('.JListTable'));
                if (_this.handleEdit) {
                    _this.handleEdit.apply(_this, [id, resp]);
                }
            });
        },
        handleList: function (params) {
            var _this = this,
                isTabActive = false,
                hash = _.parse_hash(params);
            _.defaults(hash, {p: 1});
            //处理table
            _this.$('.JListTable').addClass('loading');
            if (_this.getList) {
                _this.params = hash;
                if (_this.tpl)
                    _this.setAppContent(_this.tpl, {});
                _this.handlePage();
                _this.parseTab();
                _this.getList(hash);
            } else {
                console.warn('需要定义getList函数!');
            }
            BaseView.prototype.handleRouter.apply(_this, [params]);
        },
        renderList: function () {
            var _this = this;
            _this.$('.JListTable').removeClass('loading');
            try {
                if (_this.pageWrap) {
                    _this.page = new Pagination({wrap: _this.pageWrap});
                    _.defaults(_this.params, _this.$('.JNavTabs a:eq(0)').data());
                    _this.data.page.baseUrl = "#list&" + _.http_build_query(_.omit(_this.params, 'act', 'p'));
                    _this.page.show(_this.data.page);
                }
                var tpl = _this.itemTpl;

                if (_.isFunction(_this.itemTpl)) {
                    var params = _.parse_hash();
                    _.defaults(params, _this.$('.JNavTabs .active a').data());
                    tpl = _this.itemTpl(params);
                }
                _this.render(tpl, _this.data, _this.getListRender());

                var colspan = _this.$('.JListTable thead th').size();
                if (_.empty(_this.data.list)) {
                    if (colspan > 0) {
                        _this.render(EmptyTpl, {colspan: colspan}, _this.getListRender());
                    }
                }
            } catch (e) {
            }

            _this.appResize();
            _this.lazyload();
            if (_.in_array(_global.m, ['mp_page', 'goods'])) {
                $('.sub-menu a[data-m="' + _global.m + '"][data-a="' + _global.a + '"]').parents('li').addClass('current');
            }
            //切换
            _this.toggleHandle();
            if (_this.bindEvents)
                _this.bindEvents();
        },
        toggleHandle: function () {
            var _this = this;
            _this.$('.JListTable .JToggle').click(function () {
                var $this = $(this);
                var field = _.empty($(this).data('field')) ? 'status' : $(this).data('field');
                var val = _.intval($(this).data('val')) == 0 ? 1 : 0;
                utils.ajaxPost({
                    act: 'toggle',
                    val: val,
                    field: field,
                    id: $this.parents('tr').data('id')
                }, function () {
                    $this.data('val', val);
                    if (val) {
                        $this.removeClass('icon-times');
                        $this.addClass('icon-check');
                    } else {
                        $this.removeClass('icon-check');
                        $this.addClass('icon-times');
                    }
                });
            });
        },
        getListRender: function () {
            var _this = this;
            if (_this.$('.JListTable tbody').size() > 0) {
                return $('.JListTable tbody');
            } else {
                return $('.JListTable');
            }
        }
    });
    return BaseListView;
});